﻿//------------------------------------------------------------
// Game Framework
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
// Homepage: https://gameframework.cn/
// Feedback: mailto:ellan@gameframework.cn
//------------------------------------------------------------

using GameFramework;
using SuperScrollView;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityGameFramework.Runtime;

namespace Adventure
{
    public class BackpackerForm : UGuiForm
    {

        private LoopListView2 mScrollView;
        private ScrollRect mScrollRect;

        private List<BackpackerData> datas;

        protected override void OnInit(object userData)
        {
            base.OnInit(userData);

            datas = BackpackerData.Initialize();

            mScrollView = transform.FindTransform<LoopListView2>("Scroll View");
            mScrollView.InitListView(datas.Count, GetScrollViewUpdate);

            mScrollView.SupportScrollBar = false;
            mScrollView.ItemSnapEnable = false;

            mScrollRect = mScrollView.GetComponent<ScrollRect>();
            mScrollRect.horizontal = false;
            mScrollRect.vertical = true;
            mScrollRect.horizontalScrollbar = null;
            mScrollRect.verticalScrollbar = null;

            transform.FindTransform<UIButton>("MaskPanel").onClick.AddListener(() =>
            {
                GameEntry.Sound.PlayUISound(10001);
                GameEntry.UI.OpenUIForm(UIFormId.MenuForm,GameEntry.Procedure.CurrentProcedure);
                Close();
            });
        }
        protected override void OnOpen(object userData)
        {
            base.OnOpen(userData);

            datas = BackpackerData.Initialize();
            mScrollView.SetListItemCount(datas.Count,false);
            mScrollView.MovePanelToItemIndex(0,0);
            mScrollView.RefreshAllShownItem();
        }

        private LoopListViewItem2 GetScrollViewUpdate(LoopListView2 listView, int index)
        {
            if (index < 0 || index >= datas.Count) return null;

            var Item2 = listView.NewListViewItem("Item");

            //item2对其获取或者添加 不能直接引用，需要 dot出其gameObject，要不然分不清哪个是哪个，重复利用的情况。dmeo示例里面是直接手动添加Item，所以不存在这个问题。
            //注意：再次之前需要先获取到数据！！！，不能用一个空的数据代替着先初始化
            BackpackerItem scriptItem = Item2.gameObject.GetComponent<BackpackerItem>();
            if (scriptItem == null) scriptItem = Item2.gameObject.AddComponent<BackpackerItem>();

            if (Item2.IsInitHandlerCalled == false)
            {
                Item2.IsInitHandlerCalled = true;
                scriptItem.Init();
            }
            scriptItem.SetItemData(datas[index], index);

            return Item2;
        }
    }

    public class BackpackerItem : MonoBehaviour
    {

        public BackpackerData data;

        public Text NameText;
        public Text NumText;

        public Text InfoText;
        public UIButton uIButton;
        public void Init()
        {
            NameText = transform.FindTransform<Text>("NameText");
            NumText = transform.FindTransform<Text>("NumText");
            InfoText = transform.FindTransform<Text>("InfoText");

            uIButton = transform.FindTransform<UIButton>("UserButton");
            uIButton.onClick.AddListener(() =>
            {
                GameEntry.Sound.PlayUISound(10001);

                if (data.num <= 0)
                {
                    string content1 = GameEntry.Localization.GetString("BackpackerForm.UserErro", NameText.text);
                    int colorCode1 = 1;
                    GameEntry.UI.OpenUIForm(UIFormId.TipForm, Tuple.Create(content1, colorCode1));
                    GameEntry.TimerComponent.AddOnceTimer(1000, () => { GameEntry.UI.CloseUIForm(GameEntry.UI.GetUIForm(UIFormId.TipForm)); });
                    return;
                }

                string content = GameEntry.Localization.GetString("BackpackerForm.UserSucceed", NameText.text);
                int colorCode = 2;
                GameEntry.UI.OpenUIForm(UIFormId.TipForm, Tuple.Create(content, colorCode));


                //成就
                GameEntry.ArchiveData.Data.BackpackerCount++;
                

                if (data.Name == "Common.YaoShui_10")
                {
                    GameEntry.ArchiveData.Data.player.CurrentHP += 10;
                    data.num= --GameEntry.ArchiveData.Data.playerBackpacker.YaoShuiNum_10;
                }
                if (data.Name == "Common.YaoShui_20")
                {
                    GameEntry.ArchiveData.Data.player.CurrentHP += 20;
                    data.num = --GameEntry.ArchiveData.Data.playerBackpacker.YaoShuiNum_20;
                }
                if (data.Name == "Common.YaoShui_50")
                {
                    GameEntry.ArchiveData.Data.player.CurrentHP += 50;
                    data.num = --GameEntry.ArchiveData.Data.playerBackpacker.YaoShuiNum_50;
                }
                if (data.Name == "Common.EXP_10")
                {
                    GameEntry.ArchiveData.Data.player.CurrentEXP += 10;
                    data.num = --GameEntry.ArchiveData.Data.playerBackpacker.EXPNum_10;
                }
                if (data.Name == "Common.EXP_20")
                {
                    GameEntry.ArchiveData.Data.player.CurrentEXP += 20;
                    data.num = --GameEntry.ArchiveData.Data.playerBackpacker.EXPNum_20;
                }
                if (data.Name == "Common.EXP_50")
                {
                    GameEntry.ArchiveData.Data.player.CurrentEXP += 50;
                    data.num = --GameEntry.ArchiveData.Data.playerBackpacker.EXPNum_50;
                }



                GameEntry.TimerComponent.AddOnceTimer(1000, () => { GameEntry.UI.CloseUIForm(GameEntry.UI.GetUIForm(UIFormId.TipForm)); });
            });
        }

        public void SetItemData(object arg, int index)
        {
            var vo = (BackpackerData)arg;
            data = vo;
            NameText.text = GameEntry.Localization.GetString(vo.Name);
            NumText.text = GameEntry.Localization.GetString("BackpackerForm.Num", vo.num);
            InfoText.text = GameEntry.Localization.GetString(vo.Info);

           
        }
        private void Update()
        {
            if (data.num <= 0)
            {
               
                uIButton.gameObject.SetActive(false);
            }
            else
            {
               
                uIButton.gameObject.SetActive(true);
            }
            NumText.text = GameEntry.Localization.GetString("BackpackerForm.Num", data.num);

        }
    }
    public struct BackpackerData
    {
        public int num;
        public string Name;
        public string Info;

        public static List<BackpackerData> Initialize()
        {
            var vo = new List<BackpackerData>();
            if (GameEntry.ArchiveData.Data.playerBackpacker.YaoShuiNum_10>0)
            {
                vo.Add(new BackpackerData()
                {
                    Name = "Common.YaoShui_10",
                    num = GameEntry.ArchiveData.Data.playerBackpacker.YaoShuiNum_10,
                    Info = "Common.YaoShuiInfo_10",

                });
            }
            if (GameEntry.ArchiveData.Data.playerBackpacker.YaoShuiNum_20>0)
            {
                vo.Add(new BackpackerData()
                {
                    Name = "Common.YaoShui_20",
                    num = GameEntry.ArchiveData.Data.playerBackpacker.YaoShuiNum_20,
                    Info = "Common.YaoShuiInfo_20",

                });
            }
            if (GameEntry.ArchiveData.Data.playerBackpacker.YaoShuiNum_50>0)
            {
                vo.Add(new BackpackerData()
                {
                    Name = "Common.YaoShui_50",
                    num = GameEntry.ArchiveData.Data.playerBackpacker.YaoShuiNum_50,
                    Info = "Common.YaoShuiInfo_50",

                });
            }


            if (GameEntry.ArchiveData.Data.playerBackpacker.EXPNum_10>0)
            {
                vo.Add(new BackpackerData()
                {
                    Name = "Common.EXP_10",
                    num = GameEntry.ArchiveData.Data.playerBackpacker.EXPNum_10,
                    Info = "Common.EXPInfo_10",

                });
            }
            if (GameEntry.ArchiveData.Data.playerBackpacker.EXPNum_20>0)
            {
                vo.Add(new BackpackerData()
                {
                    Name = "Common.EXP_20",
                    num = GameEntry.ArchiveData.Data.playerBackpacker.EXPNum_20,
                    Info = "Common.EXPInfo_20",

                });
            }

            if (GameEntry.ArchiveData.Data.playerBackpacker.EXPNum_50>0)
            {
                vo.Add(new BackpackerData()
                {
                    Name = "Common.EXP_50",
                    num = GameEntry.ArchiveData.Data.playerBackpacker.EXPNum_50,
                    Info = "Common.EXPInfo_50",

                });
            }

        
      
        
            return vo;
        }

  
    }

}


